<?php

namespace App\Models;

use App\Services\Contracts\AccountServiceInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

/**
 * App\Models\Account
 *
 * @property int $id
 * @property int $uid 会员ID
 * @property float $balance 账户余额
 * @property float $total_income 累计收入
 * @property float $total_cost 累计支出
 * @property int $integral
 * @property-read \App\Models\User $user
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Account newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Account newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Account query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Account whereBalance($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Account whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Account whereIntegral($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Account whereTotalCost($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Account whereTotalIncome($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Account whereUid($value)
 * @mixin \Eloquent
 */
class Account extends Model
{
    protected $table = 'account';
    protected $primaryKey = 'id';
    protected $fillable = ['uid', 'balance', 'total_income', 'total_cost', 'points'];

    public $timestamps = false;

    public static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub

        static::creating(function (Account $account) {
            if (!$account->uid) $account->uid = Auth::id();
        });
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(User::class, 'uid', 'uid');
    }

    /**
     * @param $amount
     * @return mixed
     */
    public function incrementBalance($amount)
    {
        return app(AccountServiceInterface::class)->incrementBalance($this, $amount);
    }

    /**
     * @param $amount
     * @return mixed
     */
    public function decrementBalance($amount)
    {
        return app(AccountServiceInterface::class)->decrementBalance($this, $amount);
    }

    /**
     * @param $amount
     * @return mixed
     */
    public function incrementFreeze($amount)
    {
        return app(AccountServiceInterface::class)->incrementFreeze($this, $amount);
    }

    /**
     * @param $amount
     * @return mixed
     */
    public function decrementFreeze($amount)
    {
        return app(AccountServiceInterface::class)->decrementFreeze($this, $amount);
    }

    /**
     * @param $amount
     * @return mixed
     */
    public function freeze($amount)
    {
        return app(AccountServiceInterface::class)->freeze($this, $amount);
    }

    /**
     * @param $amount
     * @return mixed
     */
    public function unFreeze($amount)
    {
        return app(AccountServiceInterface::class)->unFreeze($this, $amount);
    }
}
